package org.yelongframework.spring.web.servlet.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.View;
import org.yelongframework.lang.Nullable;

/**
 * 响应文本的实体
 * 
 * @since 1.0.0
 */
public class TextView implements View {

	public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";

	@Nullable
	private final String contentType;

	@Nullable
	private final String content;

	public TextView(@Nullable String content) {
		this(content, DEFAULT_CONTENT_TYPE);
	}

	public TextView(@Nullable String content, @Nullable String contentType) {
		this.content = content;
		this.contentType = contentType;
	}

	@Override
	public String getContentType() {
		return contentType;
	}

	@Override
	public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (null != contentType) {
			response.setContentType(contentType);
		}
		if (null == content) {
			response.getWriter().write("");
		} else {
			response.getWriter().write(content);
		}
	}

}
